<template>
  <div class="integral-editor">
    <el-dialog
      title="编辑分类"
      width="600px"
      :visible.sync="visible"
      append-to-body
      @before-close="close()"
      :close-on-click-modal="false"
      :modal-append-to-body="true"
      :close-on-press-escape="false">
      <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="120px">
        <el-form-item prop="name" label="积分名称">
          <el-input v-model="dataForm.name"/>
        </el-form-item>

        <el-form-item prop="brief" label="积分简介">
          <el-input v-model="dataForm.brief"/>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button type="default" @click="close()">取消</el-button>
        <el-button type="primary" @click="save()" :loading="loading">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'integral-addor',
    data () {
      return {
        visible: false,
        loading: false,
        dataForm: {
          id: '',
          name: '',
          brief: '',
        },
        rules: {
          name: [{required: true, message: '请填写分类名称'}],
          brief: [{required: true, message: '请填写分类简介'}]
        },
      }
    },
    methods: {
      init (id) {
        this.loading = false
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
          if (id) {
            this.classificationInfo(id)
          }
        })
      },
      close () {
        this.visible = false
      },
      save () {
        this.loading = true
        this.$http({
          url: this.$http.adornUrl(
            `/integral-admin/integra/child/save`
          ),
          // ${!this.dataForm.id ? 'save' : 'update'}
          method: 'post',
          data: ({
            id: this.dataForm.id,
            name: this.dataForm.name,
            brief: this.dataForm.brief
          })
        }).then(res => {
          if (res.code === 200) {
            this.$message.success('保存分类成功')
            this.$emit('refresh')
            this.close()
            this.loading = true
          } else {
            this.loading = false
          }
        })
      },
      classificationInfo (cateId) {
        this.$http({
          url: this.$http.adornUrl(
            `/integral-admin/integral/cate/info/${cateId}`
          ),
          method: 'post'
        }).then(res => {
          console.log(res)
          this.dataForm = res.data
        })
      }
    }
  }
</script>

<style scoped>

</style>
